package com.financial.token.service.impl;

import com.financial.token.dao.PlatformTokenDao;
import com.financial.token.domain.PlatformTokenDO;
import com.financial.token.service.PlatformTokenService;
import com.financial.token.utils.JsonUtils;
import com.financial.token.utils.JwtUtils;
import com.financial.token.utils.RedisUtils;
import com.financial.token.utils.apiresult.ApiResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述
 *
 * @author WANGNING
 * @date Create in 2019/10/28 16:39
 * @email wyn20140226@aliyun.com
 * @des
 * @tar 调用方
 */
@Service
public class PlatformTokenServiceImpl implements PlatformTokenService {
    public static final Logger logger = LoggerFactory.getLogger(PlatformTokenServiceImpl.class);

    @Autowired
    private PlatformTokenDao platformTokenDao;
    @Autowired
    private RedisUtils redisUtils;

    /**
     *
     * @param requestJson
     * @return String
     */
    @Override
    public String getAccessToken(String requestJson) {
        logger.info("PlatformTokenServiceImpl.getAccessToken()-----获取token的时候接收到的参数为:" + requestJson);
        /*将json数据转换成map*/
        Map<String,Object> requestMap =JsonUtils.jsonToMap(requestJson);
        Map<String, Object> reqMap = new HashMap<>();
        /*获取对接平台信息*/
        String platfromNo = requestMap.get("platfrom_no").toString();
        String userName = requestMap.get("user_name").toString();
        String passWord = requestMap.get("pass_word").toString();
        String secretKey = requestMap.get("secret_key").toString();
        String salt = requestMap.get("salt").toString();
        String sign = requestMap.get("sign").toString();

        /*非空校验*/
        if(StringUtils.isEmpty(platfromNo)|| StringUtils.isEmpty(passWord)
                || StringUtils.isEmpty(secretKey) ||StringUtils.isEmpty(sign)
                ||StringUtils.isEmpty(salt)||StringUtils.isEmpty(userName)
        ){
            logger.info("PlatformTokenServiceImpl.getAccessToken()-----获取token参数为空,请检查！" );
            return String.valueOf(ApiResult.failure("获取token参数为空，请检查！"));
        }
        /*先到数据库验证*/
        PlatformTokenDO platformTokenDO= this.getPlatfromConfig(userName,passWord);
        if (null == platformTokenDO) {
            logger.info("PlatformTokenServiceImpl.getAccessToken()-----用户名密码不正确！请检查！" );
            return String.valueOf(ApiResult.failure("用户名密码不正确！"));
        }

        /*平台名称+用户名+密码+秘钥*/
        String accessSign = JwtUtils.md5Hex(platformTokenDO.getPlatfromMarking() + platformTokenDO.getUserName()+ platformTokenDO.getSalt()+platformTokenDO.getPassWord() + secretKey);
        if (!sign.equals(accessSign)) {
            logger.info("PlatformTokenServiceImpl.getAccessToken()-----验证签名失败！" );
            return String.valueOf(ApiResult.failure("验证签名失败！"));

        }
        Long expire = 60 * 1000 * 120L;
        /*给用户jwt加密生成token*/
        int time = 60 * 60 * 2;
        String financialAccessToken = JwtUtils.sign(secretKey, expire).toString();
        redisUtils.delKey(financialAccessToken);
        redisUtils.setKey("financialAccessToken", JsonUtils.objectToJson(platformTokenDO), time);
        /*封装成对象返回给客户端*/
        reqMap.put("financialAccessToken", financialAccessToken);
        reqMap.put("time", time);
        logger.info("PlatformTokenServiceImpl.getAccessToken()-----返回给调用方的信息为:" +JsonUtils.beanToJson(ApiResult.success(reqMap)));
        return JsonUtils.beanToJson(ApiResult.success(reqMap));
    }

    @Override
    public PlatformTokenDO getPlatfromConfig(String userName, String passWord) {

        return platformTokenDao.getPlatfromConfig(userName,passWord);
    }
}
